﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System;
using HouseKeepingPL.HKService;

namespace HouseKeepingPL.ViewModel
{
    ///// <summary>
    ///// This class contains properties that the main View can data bind to.
    ///// <para>
    ///// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    ///// </para>
    ///// <para>
    ///// You can also use Blend to data bind with the tool's support.
    ///// </para>
    ///// <para>
    ///// See http://www.galasoft.ch/mvvm
    ///// </para>
    ///// </summary>
    //public class MainViewModel : ViewModelBase
    //{
    //    /// <summary>
    //    /// Initializes a new instance of the MainViewModel class.
    //    /// </summary>
    //    public MainViewModel()
    //    {
    //        ////if (IsInDesignMode)
    //        ////{
    //        ////    // Code runs in Blend --> create design time data.
    //        ////}
    //        ////else
    //        ////{
    //        ////    // Code runs "for real"
    //        ////}
    //    }
    //}

    public class MainViewModel : ViewModelBase
    {      
        private ObservableCollection<Income> incomesList;
        private ObservableCollection<Outcome> outcomesList;
        private ObservableCollection<ActionType> actionTypes;
        private bool isNewIncomeItem = true;
        private bool isNewOutcomeItem = true;
        private bool cmdActionFlag = false;

        private User currentUser = null;

        public MainViewModel()
        {
            IsLoggedIn = false;
            User currentUser = new User();

            #region RelayCommands
            AuthLogin = new RelayCommand(() => cmdAuthLoginExcecute(), () => IsLoggedInNeg);
            cmdLogOut = new RelayCommand(() => cmdLogOutExcecute(), () => IsLoggedIn);
            cmdAbout = new RelayCommand(() => cmdAboutExcecute(), () => true);


            cmdApplicationExit = new RelayCommand(() => cmdApplicationExitExcecute(), () => true);



            cmdCleanIncomeInput = new RelayCommand(() => cmdCleanIncomeInputExcecute(), () => IsIncomeItemSelected());
            cmdAddIncomeRow = new RelayCommand(() => cmdAddIncomeRowExcecute(), () => !IsIncomeItemSelected());
            cmdDeleteIncomeRow = new RelayCommand(() => cmdDeleteIncomeRowExcecute(), () => IsIncomeItemSelected());
            cmdUpdateIncomeRow = new RelayCommand(() => cmdUpdateIncomeRowExcecute(), () => IsIncomeItemSelected());

            cmdCleanOutcomeInput = new RelayCommand(() => cmdCleanOutcomeInputExcecute(), () => IsOutcomeItemSelected());
            cmdAddOutcomeRow = new RelayCommand(() => cmdAddOutcomeRowExcecute(), () => !IsOutcomeItemSelected());
            cmdDeleteOutcomeRow = new RelayCommand(() => cmdDeleteOutcomeRowExcecute(), () => IsOutcomeItemSelected());
            cmdUpdateOutcomeRow = new RelayCommand(() => cmdUpdateOutcomeRowExcecute(), () => IsOutcomeItemSelected());

            cmdGetIncomeReport = new RelayCommand(() => cmdGetIncomeReportExcecute(), () => true);
            cmdGetOutcomeReport = new RelayCommand(() => cmdGetOutcomeReportExcecute(), () => true);
            cmdGetBalanceReport = new RelayCommand(() => cmdGetBalanceReportExcecute(), () => true);
            #endregion
        }





        private void InitLists()
        {
            #region init list
            incomesList = new ObservableCollection<Income>()
            {
                new Income ()
                {
                    ID = 1,
                    UserID = 1,
                    Date = new DateTime(2012,5,5),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 1
                },
                new Income ()
                {
                    ID = 2,
                    UserID = 1,
                    Date = new DateTime(2012,5,4),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 2
                },
                new Income ()
                {
                    ID = 3,
                    UserID = 1,
                    Date = new DateTime(2012,5,3),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 3
                },
                new Income ()
                {
                    ID = 4,
                    UserID = 1,
                    Date = new DateTime(2012,6,2),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 5
                }
            };

            outcomesList = new ObservableCollection<Outcome>()
            {
                new Outcome ()
                {
                    ID = 1,
                    UserID = 1,
                    Date = new DateTime(2012,1,1),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 6
                },
                new Outcome ()
                {
                    ID = 2,
                    UserID = 1,
                    Date = new DateTime(2012,5,6),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 4
                },
                new Outcome ()
                {
                    ID = 3,
                    UserID = 1,
                    Date = new DateTime(2012,5,8),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 2
                },
                new Outcome ()
                {
                    ID = 4,
                    UserID = 1,
                    Date = new DateTime(2012,5,1),
                    Comment = "bla",
                    Amount = 1000,
                    ActionTypeID = 4
                }
            };

            actionTypes = new ObservableCollection<ActionType>()
            {
                null,

                new ActionType()
                {
                    ID = 1,
                    Type = "Electronics"
                },
                new ActionType()
                {
                    ID = 2,
                    Type = "Clothing"
                },
                new ActionType()
                {
                    ID = 3,
                    Type = "Food"
                },
                new ActionType()
                {
                    ID = 4,
                    Type = "Insurance"
                },
                new ActionType()
                {
                    ID = 5,
                    Type = "Transport"
                },
                new ActionType()
                {
                    ID = 6,
                    Type = "Salary"
                },
                new ActionType()
                {
                    ID = 7,
                    Type = "Jobs"
                }

            };

            #endregion
        }

        private bool IsIncomeItemSelected()
        {
            return (!isNewIncomeItem);
        }

        private bool IsOutcomeItemSelected()
        {
            return (!isNewOutcomeItem);
        }



        #region Income



        private void cmdUpdateIncomeRowExcecute()
        {
            cmdActionFlag = true;
            if (!CheckIfIncomeInputEqGrid())
            {
                if ((MessageBox.Show("Are you sure you want to Update?", "Please confirm.", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                {
                    int indexOfUpdatedIncome = incomesList.IndexOf(CurrentlySelectedIncome);
                    incomesList[indexOfUpdatedIncome] = CurrentlySelectedIncomeInput;
                    cmdCleanIncomeInputExcecute();
                }
            }
            else
                MessageBox.Show("No changes Detected");


            cmdActionFlag = false;
        }

        private bool CheckIfIncomeInputEqGrid()
        {
            if ((CurrentlySelectedIncome.ID == CurrentlySelectedIncomeInput.ID) &&
                (CurrentlySelectedIncome.Date == CurrentlySelectedIncomeInput.Date) &&
                (CurrentlySelectedIncome.Comment == CurrentlySelectedIncomeInput.Comment) &&
                (CurrentlySelectedIncome.ActionTypeID == CurrentlySelectedIncomeInput.ActionTypeID) &&
                (CurrentlySelectedIncome.UserID == CurrentlySelectedIncomeInput.UserID) &&
                (CurrentlySelectedIncome.Amount == CurrentlySelectedIncomeInput.Amount))
                return true;

            return false;


        }

        private void cmdDeleteIncomeRowExcecute()
        {
            if ((MessageBox.Show("Are you sure you want to delete?", "Please confirm.", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
            {
                incomesList.Remove(CurrentlySelectedIncome);
            }
        }

        private void cmdAddIncomeRowExcecute()
        {
            if (CurrentlySelectedIncomeInput != null)
                incomesList.Add(CurrentlySelectedIncomeInput);

        }

        private void cmdCleanIncomeInputExcecute()
        {
            CurrentlySelectedIncome = null;
            isNewIncomeItem = true;
            IsIncomeItemSelected();
            CurrentlySelectedIncomeInput = new Income()
            {
                Date = DateTime.Now
            };
        }

        #region Command properties
        public ICommand cmdCleanIncomeInput { get; private set; }
        public ICommand cmdAddIncomeRow { get; private set; }
        public ICommand cmdDeleteIncomeRow { get; private set; }
        public ICommand cmdUpdateIncomeRow { get; private set; }

        #endregion

        #region BindProperties

        private Income _currentlySelectedIncome;
        public Income CurrentlySelectedIncome
        {
            get
            {
                isNewIncomeItem = false;
                IsIncomeItemSelected();
                if ((this._currentlySelectedIncome != null) && (cmdActionFlag == false))
                {
                    _currentlySelectedIncomeInput = new Income() //copy ctor
                    {
                        ID = this._currentlySelectedIncome.ID,
                        UserID = this._currentlySelectedIncome.UserID,
                        Date = this._currentlySelectedIncome.Date,
                        Amount = this._currentlySelectedIncome.Amount,
                        Comment = this._currentlySelectedIncome.Comment,
                        ActionTypeID = this._currentlySelectedIncome.ActionTypeID

                    };
                }
                RaisePropertyChanged("CurrentlySelectedIncomeInput");
                return this._currentlySelectedIncome;
            }
            set
            {
                this._currentlySelectedIncome = value;

                RaisePropertyChanged("CurrentlySelectedIncome");
            }
        }

        private Income _currentlySelectedIncomeInput;
        public Income CurrentlySelectedIncomeInput
        {
            get
            {
                return this._currentlySelectedIncomeInput;
            }
            set
            {
                this._currentlySelectedIncomeInput = value;
                RaisePropertyChanged("CurrentlySelectedIncomeInput");
            }
        }
        #endregion

        #endregion

        #region Outcome



        private void cmdUpdateOutcomeRowExcecute()
        {
            cmdActionFlag = true;
            if (!CheckIfOutcomeInputEqGrid())
            {
                if ((MessageBox.Show("Are you sure you want to Update?", "Please confirm.", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                {
                    int indexOfUpdatedOutcome = outcomesList.IndexOf(CurrentlySelectedOutcome);
                    outcomesList[indexOfUpdatedOutcome] = CurrentlySelectedOutcomeInput;
                    cmdCleanOutcomeInputExcecute();
                }
            }
            else
                MessageBox.Show("No changes Detected");


            cmdActionFlag = false;
        }

        private bool CheckIfOutcomeInputEqGrid()
        {
            if ((CurrentlySelectedOutcome.ID == CurrentlySelectedOutcomeInput.ID) &&
                (CurrentlySelectedOutcome.Date == CurrentlySelectedOutcomeInput.Date) &&
                (CurrentlySelectedOutcome.Comment == CurrentlySelectedOutcomeInput.Comment) &&
                (CurrentlySelectedOutcome.ActionTypeID == CurrentlySelectedOutcomeInput.ActionTypeID) &&
                (CurrentlySelectedOutcome.UserID == CurrentlySelectedOutcomeInput.UserID) &&
                (CurrentlySelectedOutcome.Amount == CurrentlySelectedOutcomeInput.Amount))
                return true;

            return false;


        }

        private void cmdDeleteOutcomeRowExcecute()
        {
            if ((MessageBox.Show("Are you sure you want to delete?", "Please confirm.", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
            {
                outcomesList.Remove(CurrentlySelectedOutcome);
            }
        }

        private void cmdAddOutcomeRowExcecute()
        {
            if (CurrentlySelectedOutcomeInput != null)
                outcomesList.Add(CurrentlySelectedOutcomeInput);

        }

        private void cmdCleanOutcomeInputExcecute()
        {
            CurrentlySelectedOutcome = null;
            isNewOutcomeItem = true;
            IsIncomeItemSelected();
            CurrentlySelectedOutcomeInput = new Outcome()
            {
                Date = DateTime.Now
            };
        }

        #region Command properties
        public ICommand cmdCleanOutcomeInput { get; private set; }
        public ICommand cmdAddOutcomeRow { get; private set; }
        public ICommand cmdDeleteOutcomeRow { get; private set; }
        public ICommand cmdUpdateOutcomeRow { get; private set; }

        #endregion

        #region BindProperties

        private Outcome _currentlySelectedOutcome;
        public Outcome CurrentlySelectedOutcome
        {
            get
            {
                isNewOutcomeItem = false;
                IsOutcomeItemSelected();
                if ((this._currentlySelectedOutcome != null) && (cmdActionFlag == false))
                {
                    _currentlySelectedOutcomeInput = new Outcome() //copy ctor
                    {
                        ID = this._currentlySelectedOutcome.ID,
                        UserID = this._currentlySelectedOutcome.UserID,
                        Date = this._currentlySelectedOutcome.Date,
                        Amount = this._currentlySelectedOutcome.Amount,
                        Comment = this._currentlySelectedOutcome.Comment,
                        ActionTypeID = this._currentlySelectedOutcome.ActionTypeID

                    };
                }
                RaisePropertyChanged("CurrentlySelectedOutcomeInput");
                return this._currentlySelectedOutcome;
            }
            set
            {
                this._currentlySelectedOutcome = value;

                RaisePropertyChanged("CurrentlySelectedOutcome");
            }
        }

        private Outcome _currentlySelectedOutcomeInput;
        public Outcome CurrentlySelectedOutcomeInput
        {
            get
            {
                return this._currentlySelectedOutcomeInput;
            }
            set
            {
                this._currentlySelectedOutcomeInput = value;
                RaisePropertyChanged("CurrentlySelectedOutcomeInput");
            }
        }
        #endregion

        #endregion

        #region Reports

        public ICommand cmdGetIncomeReport { get; private set; }
        public ICommand cmdGetOutcomeReport { get; private set; }
        public ICommand cmdGetBalanceReport { get; private set; }


        private void cmdGetIncomeReportExcecute()
        {
            TotalReportText = "Your total Income is:\n 0";
        }

        private void cmdGetOutcomeReportExcecute()
        {
            TotalReportText = "Your total Outccome is:\n 0";
        }

        private void cmdGetBalanceReportExcecute()
        {
            TotalReportText = "Your total Balance is:\n 0";
        }

        private String _totalReportText;

        public String TotalReportText
        {
            get { return _totalReportText; }
            set
            {
                _totalReportText = value;
                RaisePropertyChanged("TotalReportText");
            }
        }

        #endregion



        #region common
        public ObservableCollection<ActionType> ActionTypes
        {
            get
            {
                return actionTypes;
            }
        }
        private ActionType _CurrentlySelectedActionType;
        public ActionType CurrentlySelectedActionType
        {
            get { return _CurrentlySelectedActionType; }
            set { _CurrentlySelectedActionType = value; }
        }

        public ObservableCollection<Income> GetIncomes
        {
            get
            {
                return incomesList;
            }
            set
            {

            }


        }
        public ObservableCollection<Outcome> GetOutcomes
        {
            get
            {
                return outcomesList;
            }
            set
            {

            }


        }


        public ICommand cmdApplicationExit { get; private set; }

        private void cmdApplicationExitExcecute()
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Exit Application", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {

                Application.Current.Shutdown();

            }

        }


        #endregion

        #region login

        public ICommand AuthLogin { get; private set; }

        private bool IsAuthenticated(User currentUser)
        {
            // get user from db
            if (currentUser.Username == currentUser.Password)
                return true;
            return false;
        }


        private void cmdAuthLoginExcecute()
        {
            currentUser = new User()
            {
                Password = loginPassword,
                Username = loginUsername,
                ID = 999
            };

            if (currentUser != null)
            {
                if (IsAuthenticated(currentUser))
                {
                    MessageBox.Show(String.Format("Hello, {0}", currentUser.Username), "Welcome!");
                    IsLoggedIn = true;
                }
                else
                    MessageBox.Show("You Shall not pass!", "Error");
            }
            else
            {

                MessageBox.Show("Error number 666", "Error");
            }

        }

        private String loginUsername;

        public String LoginUsername
        {
            get { return loginUsername; }
            set { loginUsername = value; }
        }


        private String loginPassword;

        public String LoginPassword
        {
            get { return loginPassword; }
            set { loginPassword = value; }
        }

        private bool _isLoggedIn;

        public bool IsLoggedIn
        {
            get { return _isLoggedIn; }
            set
            {
                _isLoggedIn = value;
                IsLoggedInNeg = !_isLoggedIn;
                RaisePropertyChanged("IsLoggedIn");
            }

        }
        private bool _isLoggedInNeg;

        public bool IsLoggedInNeg
        {
            get { return _isLoggedInNeg; }
            set
            {
                _isLoggedInNeg = value;
                RaisePropertyChanged("IsLoggedInNeg");
            }
        }


        public ICommand cmdLogOut { get; private set; }
        private void cmdLogOutExcecute()
        {
            MessageBox.Show(String.Format("Goodbye...{0}", currentUser.Username));
            currentUser = null;
            IsLoggedIn = false;

        }

        #endregion

        public ICommand cmdAbout { get; private set; }
        private void cmdAboutExcecute()
        {
            String aboutString = String.Format("House Keeping by Roi Katz, 2012");
            MessageBox.Show(aboutString, "About House Keeping", MessageBoxButton.OK, MessageBoxImage.Information);
        }

    }
}
